package org.hackystat.iw.projectoverviewer.page;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.hackystat.iw.projectoverviewer.ProjectOverviewerApplication;
import org.hackystat.iw.projectoverviewer.page.newproject.NewProjectPage;
import org.hackystat.iw.projectoverviewer.page.projects.ProjectsPage;
import org.hackystat.sensorbase.client.SensorBaseClient;

/**
 * Provides a base class with associated markup that all Project Overviewer pages
 * should inherit from. See http://wicket.apache.org/examplemarkupinheritance.html for an
 * explanation of how this works. This base page only provides the header and footer markup.
 * 
 * @author Philip Johnson
 * @author Jianfei Liao
 * @author Nathan Britton
 */
public class BasePage extends WebPage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Create the ProjectBrowserBasePage.
   */
  public BasePage() {
    ProjectOverviewerApplication app = (ProjectOverviewerApplication)getApplication();
    
    // add the tabs as header
    add(new BookmarkablePageLink("Home", ProjectsPage.class));
    add(new BookmarkablePageLink("NewProjectLink", NewProjectPage.class));   
    
    // add the footer
    add(new Label("application-name", app.getApplicationName()));
    add(new Label("serviceInfo", getServiceInfo()));

  }
    
  /**
   * Returns a string indicating which services are available. 
   * @return A string indicating service availability. 
   */
  private String getServiceInfo() {
    ProjectOverviewerApplication app = (ProjectOverviewerApplication)getApplication();
    
    String sensorbase = app.getSensorBaseHost();
    boolean sensorbaseOk = SensorBaseClient.isHost(sensorbase);
    StringBuffer serviceInfo = new StringBuffer();
    if (sensorbaseOk) {
      serviceInfo.append("Contacted: ");
      serviceInfo.append(sensorbaseOk ? sensorbase + " " : "");
    }
    if (!sensorbaseOk) {
      serviceInfo.append("Failed to contact: ");
      serviceInfo.append(sensorbaseOk ? "" : sensorbase + " ");
    }
    return serviceInfo.toString();
  }
}
